<template>
  <div>
    <FormPanel ref="from" :list="formList" cols="3" @formChange="changeHandle" :isSubmitBtn="false" :collapse="false" labelWidth="110" />
    <div style="height:47px;"></div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDrawer(1)">取 消</el-button>
      <multiuse-button type="primary" :click="closeDrawer.bind(this, 2)">确 定</multiuse-button>
    </div>
  </div>
</template>
<script>
import {editmajorAddress} from '@/api/omSale/KeyCustomers/PickAddress/index.js'
import { dictionary } from '@/utils/dictMixin';
export default {
  data(){
    this.saveData=null;
    return{
      formList:this.createdFrom()
    }
  },
  props:['list'],
  mixins:[dictionary],
  methods:{
    createdFrom(){
      return [
        {
          type: 'SELECT',
          label: '省份',
          fieldName: 'provinceId',
          placeholder: '请选择',
          itemList:this.createRegionsList(),
          rules: [{ required: true, message: '请选择', trigger: 'change' }],
          change:(p)=>{
            if(p){
              this.formList.map(x=>{
                if(x.fieldName=='cityId'){
                  x.itemList=this.createcityList(p)
                }
              })
            }else{
              this.formList.map(x=>{
                if(x.fieldName=='cityId'){
                  x.itemList=[];
                  x.initialValue=''
                }
                if(x.fieldName=='districtId'){
                  x.itemList=[];
                  x.initialValue='';
                }
              })
            }

          }
        },
        {
          type: 'SELECT',
          label: '城市',
          fieldName: 'cityId',
          placeholder: '请选择',
          itemList:[],
          rules: [{ required: true, message: '请选择', trigger: 'change' }],
          change:(p)=>{
            if(p){
              this.formList.map(x=>{
              if(x.fieldName=='districtId'){
                x.itemList=this.district(p)
              }
            })
            }else{
              this.formList.map(x=>{
              if(x.fieldName=='districtId'){
                x.itemList=[];
                x.initialValue='';
              }
            })
            }

          }
        },
        {
          type: 'SELECT',
          label: '县区',
          fieldName: 'districtId',
          placeholder: '请选择',
          itemList:[],
          rules: [{ required: true, message: '请选择', trigger: 'change' }],
        },
        {
          type: 'INPUT',
          label: '收车地址',
          fieldName: 'afterAddress',
          placeholder: '请输入',
          rules: [{ required: true, message: '请输入', trigger: 'blur' }],
        },
        {
          type: 'INPUT',
          label: '联系人',
          fieldName: 'afterLinkMan',
          placeholder: '请输入',
          rules: [{ required: true, message: '请输入', trigger: 'blur' }],
        },
        {
          type: 'INPUT',
          label: '联系人电话',
          fieldName: 'afterLinkPhone',
          placeholder: '请输入',
          rules: [{ required: true, message: '请输入', trigger: 'blur' }],
        },


      ]
    },
    changeHandle(val){
      this.saveData=val;
    },
    async closeDrawer(type){
      if(type==1){
        this.$emit('close')
      }else{
        this.$refs.from.SUBMIT_FORM();
        console.log(this.saveData, 'this.saveData');
        if (this.saveData) {
          let json = JSON.parse(JSON.stringify(this.saveData));
          let arr=[]
          this.list.map(x=>{
            arr.push({
              deptId:x.deptId,
              deptName:x.deptName,
              officeId:x.officeId,
              officeName:x.officeName,
              dealerId:x.dealerId,
              dealerCode:x.dealerCode,
              dealerName:x.dealerName,
              orderId:x.orderId,
              orderNo:x.orderNo,
              deliveryOrderId:x.deliveryOrderId,
              doNo:x.doNo,
              orderType:x.orderType,
              orderDetailType:x.orderDetailType,
              applyStatus:x.orderStatus,
              accountType:x.accountType,
              vin:x.vin,
              productCode:x.productCode,
              productName:x.productDesc,
              powerType:x.powerType,
              provinceId:json.provinceId,
              cityId:json.cityId,
              districtId:json.districtId,
              beforeAddress:x.address,
              afterAddress:json.afterAddress,
              beforeLinkMan:x.linkMan,
              afterLinkMan:json.afterLinkMan,
              beforeLinkPhone:x.linkPhone,
              afterLinkPhone:json.afterLinkPhone,
              deliveryOrderId:x.id
            })
          })
          console.log(arr,'arr')
          let res = await editmajorAddress(arr);
          if (res.resultCode == 200) {
            this.$notify({
              title: '成功',
              message: '调整成功',
              type: 'success'
            });
            this.$emit('close', json);
          }else{
            this.saveData=null;
          }
        }
      }
    }
  }
}
</script>
